package com.gt.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gt.entity.WxReq;
import com.gt.service.WeChatRestService;
import com.gt.utils.HttpUtil;
import com.gt.utils.RSAUtilSegment;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
/**
 * 口令码发券微信相关接口实现
 */
@Service
@Slf4j
public class WeChatRestServiceImpl implements WeChatRestService {

    // 密钥长度 1024 bit ；密钥格式：PKCS#8
    // 公钥，用于服务商加密商户数据
    String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8jZkBuFY+AWs0hJ66arR0Ixbm" +
            "3lPOAgC/2fo1aS+g3rLdKtM9eucoLaPtr44blo67O1/sqFrppE+DeizslGd8bcg6" +
            "Nisc49pIWvlHHHihl3LoqQBzOKmRs5vTpqNQm6I2oyeTWjCrnbv7SGl4mHk5bqAe" +
            "QWG3W7mKnRTUBtmHYQIDAQAB";

    // 私钥，用于国通解密数据
    String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALyNmQG4Vj4BazSE" +
            "nrpqtHQjFubeU84CAL/Z+jVpL6Dest0q0z165ygto+2vjhuWjrs7X+yoWumkT4N6" +
            "LOyUZ3xtyDo2Kxzj2kha+UcceKGXcuipAHM4qZGzm9Omo1CbojajJ5NaMKudu/tI" +
            "aXiYeTluoB5BYbdbuYqdFNQG2YdhAgMBAAECgYAuETps3SjVBWu37R8K1L86EMir" +
            "p246Sur79LnZCtYEK+TvfGMYBksr2drPW7EQtHwy6pLJM2ji5K44pUUMmEWgCX+K" +
            "qvkAYMKLlmLt41TbVmbAyPCbphusXiv2vcDoyDhNwezEy7cutjPuJFGE9e+U618E" +
            "5eRyntm3KSTyqNreQQJBAOfKbrDdmXdmfrye+gZK/qXNo6MJJEhZD+hGj810kRgv" +
            "FKlfDRhF5dTpaVuPuBzdhb8mRD23G20te8Ucs591nrUCQQDQPxZeTB3nUkVqMQwz" +
            "//77lrOZ7cUkgpsW+6dUjTqKBPnHOIZgrajbInDLR/wYSDLnHYAlqttRy5gaWYdW" +
            "e4V9AkBa2JZlr5S0nXFS0CRCTyZbKavW/IBB8tYvPWijJ+quRdWdRGnHd63Ksuje" +
            "QAfzGEeRUZgxcBDJy6fU65jjCKB5AkEAll+360fToCJMda4NPc9xdIMSFekYHpEh" +
            "JKt73RZOWzPUmgM7JbzA3dw0ztWgOHiKKVGoI4sI7Du0DfLwQ4bWkQJBAKQPtRfv" +
            "Mx7lGSJIr2q5/dZAYJMiYBaC0cgnaxcQphuhNKpSB3jE6O3DljNq8/RuUh7RZOqG" +
            "uZO5bEI+3+X3sUE=";
    @Value("${WX_GUOMI_URL}")
    private String WX_GUOMI_URL;
    @Autowired
    private RestTemplate restTemplate;
    /**
     * wjh
     * 2021/9/20
     * 转发第三方接口的请求给微信
     */
    @Override
    public JSONObject baseWxRest(String url, String method, String body) throws Exception {
        URIBuilder uriBuilder = new URIBuilder(url);
        URI uri = uriBuilder.build();

        JSONObject reqJson = new JSONObject();
        reqJson.put("url",uri.toString());
        reqJson.put("method",method);
        reqJson.put("body",body);
        JSONObject resJson = HttpUtil.requestApiPost(new String(org.apache.commons.codec.binary.Base64.encodeBase64(RSAUtilSegment.encryptByPublicKey(reqJson.toJSONString().getBytes(), publicKey)))
                , WX_GUOMI_URL, restTemplate);
        if("000000".equals(resJson.getString("code"))){
//            return JSONObject.parseObject(new String(RSAUtilSegment.decryptByPrivateKey(Base64.decodeBase64(resJson.getString("data")), privateKey)));
            return resJson.getJSONObject("data");
        }else{
            return resJson;
        }
    }

    @Override
    public JSONObject baseWxRest(WxReq wxReq) throws Exception {
        return baseWxRest(wxReq.getUrl(), wxReq.getMethod(), wxReq.getMethod());
    }
}
